package com.month.order.service.impl;

import com.month.order.bean.Order;
import com.month.order.feign.ProductFeignClient;
import com.month.order.service.OrderService;
import com.month.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    RestTemplate  restTemplate;


    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    public Order createOrder(Long productId, Long userId) {

        Product product = productFeignClient.getProductById(productId);

        Order order = new Order();
        order.setId(1L);
        order.setUserId(userId);
        order.setNickName("month");
        order.setAddress("西安");
        //总金额需要计算-TODO
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getPurchaseNum())));
        //TODO-远程调用商品服务查询商品信息
        order.setProductList(Arrays.asList(product));
        return order;
    }



    /**
     * 负载均衡
     * @param productId
     * @return
     */
    private Product getProductFromRemoteWithLoadBalancer(Long productId) {
        //远程url
        String url = "http://service-product"+ "/getProduct/" + productId;
        //2.调用接口
        return restTemplate.getForObject(url, Product.class);
    }
}
